package com.cdn.allmobilestore.loader;

import android.content.Context;
import java.util.ArrayList;
import org.json.JSONObject;

import com.cdn.allmobilestore.model.AppInfo;
import com.cdn.allmobilestore.model.ListAppInfo;
import com.cdn.allmobilestore.ulti.Constant;
import com.cdn.allmobilestore.ulti.Log;
import com.cdn.allmobilestore.ulti.Request;

public class ListAppLoader extends BaseLoader
{
	private String catid;
	private String listapp_type;
	public boolean mNeedNextPage;
	private int mPage = 1;
	private String mAgencyId;
	
    public ListAppLoader(Context paramContext)
    {
        super(paramContext);
        this.mNeedNextPage = false;
        //Log.LOGTAG = "ListAppLoader";
    }
    
    public void setmPage(int page)
    {
    	mPage = page;
    }
    
    
    public void setmAgencyId(String agency_id)
    {
    	mAgencyId = agency_id;
    }
    
    public void setCategoryID(String categoryid)
    {
        this.catid = categoryid;
    }
    
    public void setListAppType(String type)
    {
        this.listapp_type = type;
    }
    
    protected BaseResult getResultInstance()
    {
        return new Result();
    }
    
    protected BaseLoader.UpdateTask getUpdateTask()
    {
        return new TopAppUpdateTask();
        
    }
    
    protected BaseResult parseResult(JSONObject json, BaseResult result)
    throws Exception
    {
        Result localResult = (Result)result;
        localResult.mTopAppInfos = ListAppInfo.valueOfAray(json);
        return localResult;
    }
    
    private class TopAppUpdateTask extends BaseLoader.UpdateTask
    {
    	protected boolean mIsAppend;
        private TopAppUpdateTask()
        {
            super();
            if (ListAppLoader.this.mPage == 1)
            	this.mIsAppend =  false;
            else
            	this.mIsAppend =  true;
        }
        
        @Override
        protected Request getRequest() {
            // TODO Auto-generated method stub
        	//Log.v(String.format(Constant.GET_LISTAPP_PAGING_URL,ListAppLoader.this.listapp_type,ListAppLoader.this.catid,mPage)  );
        	String urlRequest = "";
        	if(ListAppLoader.this.listapp_type.equals(Constant.LISTAPP_TYPE_DEVELOPER) )
        		urlRequest = String.format(Constant.GET_LISTAPP_PAGING_URL,ListAppLoader.this.listapp_type,ListAppLoader.this.mAgencyId,mPage);
        	else
        		urlRequest = String.format(Constant.GET_LISTAPP_PAGING_URL,ListAppLoader.this.listapp_type,ListAppLoader.this.catid,mPage);
        	Log.v(urlRequest);
            return new Request(urlRequest);
        }
        
        
        protected BaseResult onDataLoaded(BaseResult oldresult, BaseResult newresult)
	    {
	    	BaseResult localResult = null;
	    	
	    	if(newresult != null && (((Result)newresult).mTopAppInfos != null) && (!((Result)newresult).mTopAppInfos.isEmpty()))
	    	{
	    		ListAppLoader.this.mNeedNextPage = true;
	    		localResult = newresult;
	    		
	    		mResult = newresult.shallowClone();
	    		Log.v("this.mIsAppend:" + this.mIsAppend);
	    		if(this.mIsAppend )
		    	{
		    		localResult = merge((ListAppLoader.Result)oldresult, (ListAppLoader.Result)newresult);
		    	}
	
	    		return localResult;
	    	}
	    	
	    	
	    	return newresult;

	    }
        
        
        private ListAppLoader.Result merge(ListAppLoader.Result oldresult, ListAppLoader.Result newresult)
	    {
	      ListAppLoader.Result localResult = new ListAppLoader.Result();
	      localResult.mTopAppInfos = new ArrayList<AppInfo>();    
	        if (oldresult != null)
	          localResult.mTopAppInfos.addAll(oldresult.mTopAppInfos);
	        if (newresult != null)
	           localResult.mTopAppInfos.addAll(newresult.mTopAppInfos);
	        	       
	        
		  return localResult;
	        
	    }
        
    }
    
    
    public void reload()
	  {
	    if (!isLoading())
	    {
	      this.mPage = 0;
	      this.mNeedNextPage = false;
	      super.reload();
	    }
	  }
	
	public void nextPage() {
		if (this.mNeedNextPage)
		      this.mPage = (1 + this.mPage);
		
	}
    
    
    public static final class Result extends BaseResult
    {
        public ArrayList<AppInfo> mTopAppInfos;
        
        protected int getCount()
        {
            int count = 0;
            if (this.mTopAppInfos != null)
            count = this.mTopAppInfos.size();
            return count;
        }
        
        public BaseResult shallowClone()
        {
            Result localResult = new Result();
            localResult.mTopAppInfos = this.mTopAppInfos;
            return localResult;
        }
    }
    
    
}